package com.suray.wcs.service.pojo.rgv.move.speed;

import com.suray.basic.wcs.utils.LoggerType;
import com.suray.basic.wcs.utils.LoggerUtil;
import com.suray.basic.wcs.utils.ModuleType;
import com.suray.basic.wcs.utils.WcsFuncUtil;
import com.suray.basic.wcs.utils.thread.BaseThread;
import com.suray.wcs.service.pojo.Rgv;
import com.suray.wcs.service.rescontact.ToRes;

import java.util.HashMap;
import java.util.Map;

/**
 * 手动行驶线程
 */
public class RgvMoveBySpeedThread extends BaseThread {

    private static Map<Rgv, RgvMoveBySpeedThread> rgvMoveSpeedMap = new HashMap<>();

    private static final String VERSION_HEAD = "RES+3.1_v";

    //0.0.45 : 新手动行驶版本号
    private static final int VERSION = 0 * 10000 + 0 * 100 + 45;

    //按速度行驶状态
    private NormalActionStatus sonRgvSpeedMove;

    //按速度行驶方向
    private int direction;

    private Rgv rgv;

    private ToRes toRes;

    private int speed;

    private RgvMoveBySpeedThread(Rgv rgv) {
        super(rgv.getRgvName() + "按速度行驶", 1000, RgvMoveBySpeedThread.class);

        this.rgv = rgv;
        this.toRes = ToRes.getInstance(rgv);
        sonRgvSpeedMove = NormalActionStatus.STANDY;
    }

    /**
     * 速度模式：新版本模式还是老版本模式
     * TRUE : 新模式， FALSE : 旧模式
     */
    public static boolean isSpeedMode(Rgv rgv) {
        if (rgv.isInitOk()) {
            return rgv.versionCheck(VERSION);
        } else {
            LoggerUtil.info(ModuleType.WCS_SYSTEM, LoggerType.WARN, rgv.isInitOk(), rgv.getRgvName(),
                    "未获取到res版本，默认使用旧的按速度行驶", RgvMoveBySpeedThread.class);
            return false;
        }
    }

    public static synchronized RgvMoveBySpeedThread getInstance(Rgv rgv) {
        RgvMoveBySpeedThread rgvMoveBySpeedThread = rgvMoveSpeedMap.get(rgv);
        if (rgvMoveBySpeedThread == null) {
            rgvMoveBySpeedThread = new RgvMoveBySpeedThread(rgv);
            rgvMoveBySpeedThread.start();
            rgvMoveSpeedMap.put(rgv, rgvMoveBySpeedThread);
        }

        return rgvMoveBySpeedThread;
    }

    @Override
    protected void process() {
        try {
            while (sonRgvSpeedMove == NormalActionStatus.ACTION) {
                LoggerUtil.info(ModuleType.RGV, LoggerType.MOVE_BY_SPEED, rgv.getRgvName(), sonRgvSpeedMove,
                        "开始按速度行驶", this.getClass());
                toRes.runWithSpeed(null, direction, speed);
                WcsFuncUtil.delay(300);
            }
            if (sonRgvSpeedMove == NormalActionStatus.STOP_ACTION) {
                sonRgvSpeedMove = NormalActionStatus.STANDY;
                LoggerUtil.info(ModuleType.RGV, LoggerType.MOVE_BY_SPEED, rgv.getRgvName(), sonRgvSpeedMove,
                        "按速度停止成功", this.getClass());
            }
        } catch (Exception e) {
            LoggerUtil.info(ModuleType.RGV, LoggerType.ERROR, rgv.getRgvName(), sonRgvSpeedMove, e.getMessage(),
                    this.getClass());
            sonRgvSpeedMove = NormalActionStatus.STANDY;
        }
    }

    /**
     * 按速度停止
     */
    public void stopSpeedMove() {
        synchronized (sonRgvSpeedMove) {
            if (sonRgvSpeedMove == NormalActionStatus.ACTION) {
                sonRgvSpeedMove = NormalActionStatus.STOP_ACTION;
            }
        }
        LoggerUtil.info(ModuleType.RGV, LoggerType.MOVE_BY_SPEED, rgv.getRgvName(), sonRgvSpeedMove, "按速度停止",
                this.getClass());
    }

    /**
     * 按速度行驶
     */
    public void startSpeedMove(int direction, int speed) {
        synchronized (sonRgvSpeedMove) {
            this.direction = direction;
            this.speed = speed;
            sonRgvSpeedMove = NormalActionStatus.ACTION;
        }
        LoggerUtil.info(ModuleType.RGV, LoggerType.MOVE_BY_SPEED, rgv.getRgvName(), sonRgvSpeedMove, "按速度行驶",
                this.getClass());
    }
}
